﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;
using Celf;
//using Apollo;
//using LuaInterface;

public static class JSCommandDefine
{
    //关闭视图
    public static string cmd_closeview = "cmd_closeview";
    //查询Wifi
    public static string cmd_isWifi = "cmd_isWifi";
    //获取参数
    public static string cmd_params = "cmd_params";
}

/// <summary>
/// 根据Webview返回的参数，返回特定数据到JS层，数据格式为Json格式
/// </summary>
class WebViewParser
{
    /// <summary>
    /// 1.关闭WebView : cmd_closeview
    /// 2.查询是否使用wifi:cmd_isWifi 
    /// 3.查询当前参数
    /// </summary>
    //public static void ParseCmdFromJs(string cmd)
    //{
    //    if (string.IsNullOrEmpty(cmd))
    //    {
    //        Debug.LogError("ParseCmdFromJs: cmd from js is null");
    //        return;
    //    }

    //    if (cmd == JSCommandDefine.cmd_closeview)
    //    {
    //        WebViewService.Instance.Close();
    //    }
    //    else if (cmd == JSCommandDefine.cmd_isWifi)
    //    {
    //        Dictionary<string, string> paramDic = new Dictionary<string, string>();
    //        int iswiff = NetworkExport.GetNetWorkType();
    //        paramDic.Add("iswifi", (iswiff == 2).ToString());
    //        WebViewService.Instance.SendCommondToJs(JSCommandDefine.cmd_isWifi, paramDic);
    //    }
    //    else if (cmd == JSCommandDefine.cmd_params)
    //    {
    //        Dictionary<string, string> paramDic = new Dictionary<string, string>();
    //        ApolloAccountInfo accountinfo = NetworkAccount.GetAccountInfo();

    //        // AppId
    //        if (accountinfo.Platform == ApolloPlatform.Wechat)
    //        {
    //            paramDic.Add("appid", NetworkConfig.WXAppID);
    //        }
    //        else if (accountinfo.Platform == ApolloPlatform.QQ)
    //        {
    //            paramDic.Add("appid", NetworkConfig.QQAppID);
    //        }
    //        else
    //        {
    //            paramDic.Add("appid", "0");
    //        }
    //        //OpenId
    //        paramDic.Add("openid", accountinfo.OpenId);
    //        //Access Token
    //        paramDic.Add("access_token", accountinfo.GetToken(ApolloTokenType.Access).Value);
    //        //当前没有二级分区
    //        paramDic.Add("partition", "0");
    //        //RoleId
    //        paramDic.Add("roleid", (string) LuaService.Instance.luaState["USERID"]);

    //        WebViewService.Instance.SendCommondToJs(JSCommandDefine.cmd_params, paramDic);
    //    }
    //    else
    //    {
    //        Debug.LogError("ParseCmdFromJs：cmd is not exist");
    //    }
    //}

    //测试使用
    public static void testParseCmdFromJs(string cmd)
    {
        if (string.IsNullOrEmpty(cmd))
        {
            Debug.LogError("ParseCmdFromJs: cmd from js is null");
            return;
        }

        if (cmd == JSCommandDefine.cmd_closeview)
        {
            WebViewService.Instance.Close();
        }
        else if (cmd == JSCommandDefine.cmd_isWifi)
        {
            Dictionary<string, string> paramDic = new Dictionary<string, string>();
            paramDic.Add("Iswifi", "true");

            WebViewService.Instance.SendCommondToJs(JSCommandDefine.cmd_isWifi, paramDic);
        }
        else if (cmd == JSCommandDefine.cmd_params)
        {
            Dictionary<string, string> paramDic = new Dictionary<string, string>();
            paramDic.Add("appid", "appid_123456789");
            paramDic.Add("openid", "openid_asdsfghjkl");
            paramDic.Add("token", "token_0908987776");
            paramDic.Add("partition", "partition_111");
            paramDic.Add("roleid", "roleid_454564325");
            WebViewService.Instance.SendCommondToJs(JSCommandDefine.cmd_params, paramDic);
        }
        else
        {
            Debug.LogError("ParseCmdFromJs：cmd is not exist");
        }
    }
}